package com.rzico.account.plugin.hxBank;

import cn.hutool.core.codec.Base64;

import javax.crypto.Cipher;
import java.io.IOException;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

/**
 * Created by icssh on 2020/9/7.
 */
public class RSAUtils {
    //生成秘钥对
    public static KeyPair getKeyPair() throws Exception {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        keyPairGenerator.initialize(2048);
        KeyPair keyPair = keyPairGenerator.generateKeyPair();
        return keyPair;
    }
    //获取公钥(Base64 编码)
//    AES 和 RSA 加解密以及 RSA 加验签的组合使用
    public static String getPublicKey(KeyPair keyPair){
        PublicKey publicKey = keyPair.getPublic();
        byte[] bytes = publicKey.getEncoded();
        return byte2Base64(bytes);
    }
    //获取私钥(Base64 编码)
    public static String getPrivateKey(KeyPair keyPair){
        PrivateKey privateKey = keyPair.getPrivate();
        byte[] bytes = privateKey.getEncoded();
        return byte2Base64(bytes);
    }
    //将 Base64 编码后的公钥转换成 PublicKey 对象
    public static PublicKey string2PublicKey(String pubStr) throws Exception{
//        AES 和 RSA 加解密以及 RSA 加验签的组合使用
        byte[] keyBytes = base642Byte(pubStr);
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PublicKey publicKey = keyFactory.generatePublic(keySpec);
        return publicKey;
    }
    //将 Base64 编码后的私钥转换成 PrivateKey 对象
    public static PrivateKey string2PrivateKey(String priStr) throws Exception{
        byte[] keyBytes = base642Byte(priStr);
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
        return privateKey;
    }
//    AES 和 RSA 加解密以及 RSA 加验签的组合使用
    //将 Base64 编码后的公钥转换成 PublicKey 对象
    public static PublicKey string2PublicKey( byte[] keyBytes) throws Exception{


        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);

        KeyFactory keyFactory = KeyFactory.getInstance("RSA");

        PublicKey publicKey = keyFactory.generatePublic(keySpec);

        return publicKey;

    }
    //将 Base64 编码后的私钥转换成 PrivateKey 对象
    public static PrivateKey string2PrivateKey(byte[] keyBytes) throws Exception{


        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);

        KeyFactory keyFactory = KeyFactory.getInstance("RSA");

        PrivateKey privateKey = keyFactory.generatePrivate(keySpec);

        return privateKey;

//        AES 和 RSA 加解密以及 RSA 加验签的组合使用
    }
    //公钥加密
    public static byte[] publicEncrypt(byte[] content, PublicKey publicKey) throws Exception{
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        byte[] bytes = cipher.doFinal(content);
        return bytes;
    }
    //私钥解密
    public static byte[] privateDecrypt(byte[] content, PrivateKey privateKey) throws Exception{
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        byte[] bytes = cipher.doFinal(content);
//        AES 和 RSA 加解密以及 RSA 加验签的组合使用
        return bytes;
    }
    //字节数组转 Base64 编码
    public static String byte2Base64(byte[] bytes){
// BASE64Encoder encoder = new BASE64Encoder();
        return Base64.encode(bytes);
    }
    //Base64 编码转字节数组
    public static byte[] base642Byte(String base64Key) throws IOException {
// BASE64Decoder decoder = new BASE64Decoder();
        return Base64.decode(base64Key);
    }
    public static void main(String[] args) throws Exception {
        KeyPair keyPair = getKeyPair();
//        AES 和 RSA 加解密以及 RSA 加验签的组合使用
        System.out.println(getPublicKey(keyPair));
        System.out.println(getPrivateKey(keyPair));
    }
}
